package com.lu.nio.client;

import cn.hutool.core.util.CharsetUtil;
import com.lu.GlobalConfig;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;

/**
 * @program lu-netty
 * @description:
 * @author: zhanglu
 * @create: 2022-06-10 16:38:00
 */
@Slf4j
public class NioClient {

    public static void main(String[] args) throws IOException {
        Selector selector = Selector.open();
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        boolean isConnect = socketChannel.connect(new InetSocketAddress(GlobalConfig.LOCAL_IP, GlobalConfig.LOCAL_PORT));
        if(isConnect){
            socketChannel.register(selector, SelectionKey.OP_READ);
        }else {
            socketChannel.register(selector, SelectionKey.OP_CONNECT);
        }
        log.info("NioClient start...");
        new NioClientHandler(selector, CharsetUtil.UTF_8).start();
    }

}
